package com.yonyou.common.dialog;

import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import com.yonyou.component.MRProcess;
import com.yonyou.component.MultiComboBox;
import com.yonyou.component.MultiMrComboBox;
import git4idea.GitLocalBranch;
import git4idea.commands.Git;
import git4idea.repo.GitRepository;
import git4idea.repo.GitRepositoryManager;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.util.LinkedHashMap;

/**
 * @author wengyongcheng
 * @since 2020/3/2 10:36 下午
 */
public class MRCustomDialogWrapper extends DialogWrapper {

    private JPanel north = new JPanel();
    private JPanel center = new JPanel();
    private JPanel south = new JPanel();

    private JLabel branch  = new JLabel("分支：");
    private JLabel flag = new JLabel("注释：");


    private JLabel pwd = new JLabel("秘钥：");
    private JTextArea pwd_text= new JTextArea();

    private JTextArea flag_text= new JTextArea();
    public static int text_h=35;
    private MultiMrComboBox multiComboBox = new MultiMrComboBox();
    JButton sureBtn = new JButton("确认");
    JButton cancleBtn = new JButton("取消");


    private LinkedHashMap linkedHashMap ;
    private GitRepositoryManager manager ;
    private GitRepository repository ;
    private  Project project;

    public MRCustomDialogWrapper(Project project, GitRepositoryManager manager, GitRepository repository, LinkedHashMap linkedHashMap ) {
        super(true);
        this.project = project;
        this.repository = repository;
        this.manager = manager;
        this.linkedHashMap = linkedHashMap;
        setTitle("智能合并"); //设置会话框标题
        init(); //触发一下init方法，否则swing样式将无法展示在会话框
    }

    @Override
    protected JComponent createNorthPanel() {
        return null;
    }


    @Override
    protected JComponent createSouthPanel() {
        sureBtn.setHorizontalAlignment(SwingConstants.CENTER); //水平居中
        sureBtn.setVerticalAlignment(SwingConstants.CENTER); //垂直居中
        south.add(sureBtn, BorderLayout.EAST);

        cancleBtn.setHorizontalAlignment(SwingConstants.CENTER); //水平居中
        cancleBtn.setVerticalAlignment(SwingConstants.CENTER); //垂直居中
        south.add(cancleBtn, BorderLayout.EAST);


        //按钮事件绑定
        sureBtn.addActionListener(e -> {
            try {
                GitLocalBranch localB = repository.getCurrentBranch();
                String branchN = localB.getName();
                final String[] selectedValues = multiComboBox.getSelectedValues();
                final String text = flag_text.getText();
                final String text1 = pwd_text.getText();

                if(StringUtils.isEmpty(text)){
                    com.yonyou.ctm.ctmplugin.utils.NotificationUtil.showDialog("提示","请填写注释,不可以为空！");
                    return;
                }
                if(StringUtils.isEmpty(text1)){
                    com.yonyou.ctm.ctmplugin.utils.NotificationUtil.showDialog("提示","请填写秘钥,不可以为空！");
                    return ;
                }
                Git git = ServiceManager.getService(Git.class);
                ProgressManager.getInstance().run(new MRProcess(linkedHashMap,repository,git,project, "合并请求中",text,text1,selectedValues));
                this.close(DialogWrapper.CANCEL_EXIT_CODE);
            } catch (Exception ex) {
                this.close(DialogWrapper.CANCEL_EXIT_CODE);
                com.yonyou.ctm.ctmplugin.utils.NotificationUtil.showDialog("异常提示",ex.getMessage());
            }
        });
        cancleBtn.addActionListener(e -> {
            this.close(DialogWrapper.CANCEL_EXIT_CODE);
        });

        return south;
    }

    @Override
    protected JComponent createCenterPanel() {

        final GridBagLayout gridLayout = new GridBagLayout();
        center.setLayout(gridLayout);


        GridBagConstraints g1 = new GridBagConstraints();
        g1.gridx=1;
        g1.gridy=1;

        //row1：按钮事件触发后将结果打印在这里
        branch.setForeground(new Color(255, 47, 93)); //设置字体颜色
        branch.setHorizontalAlignment(SwingConstants.CENTER);
        center.add(branch,g1);


        GridBagConstraints g2 = new GridBagConstraints();
        g2.gridx=2;
        g2.gridy=1;
        multiComboBox.initComponent(linkedHashMap);
        center.add(multiComboBox,g2);


        GridBagConstraints g3 = new GridBagConstraints();
        g3.gridx=1;
        g3.gridy=2;
        g3.insets = new Insets(20,0,20,0);

        flag.setForeground(new Color(139, 181, 20)); //设置字体颜色
        flag.setHorizontalAlignment(SwingConstants.CENTER);
        flag.setVerticalAlignment(SwingConstants.TOP);
        center.add(flag,g3);

        GridBagConstraints g4 = new GridBagConstraints();
        g4.gridx=2;
        g4.gridy=2;
        g4.insets = new Insets(20,0,20,0);
        flag_text.setLineWrap(true);//设置换行
        flag_text.setPreferredSize(new Dimension(220,80));
        center.add(flag_text,g4);




        GridBagConstraints g5 = new GridBagConstraints();
        g5.gridx=1;
        g5.gridy=3;
        g5.insets = new Insets(20,0,20,0);
        pwd.setForeground(new Color(139, 181, 20)); //设置字体颜色
        pwd.setHorizontalAlignment(SwingConstants.CENTER);
        pwd.setVerticalAlignment(SwingConstants.TOP);
        center.add(pwd,g5);

        GridBagConstraints g6 = new GridBagConstraints();
        g6.gridx=2;
        g6.gridy=3;
        g6.insets = new Insets(20,0,20,0);
        pwd_text.setLineWrap(true);//设置换行
        pwd_text.setPreferredSize(new Dimension(220,40));
        center.add(pwd_text,g6);
        return center;
    }

}

